<template>
	<view class="setting">
		<view class="cartType">
			<view class="cartType_item" @click="setAvatar">
				<u-row>
					<u-col :span="2">
						<view style="margin-top:2.5vh ;">
							头像	
						</view>
					</u-col>
					<u-col :span="7">
						<view style="height: 1px; width: 100%;"></view>
					</u-col>
					<u-col :span="3">
						<view style="margin: 0 auto;width: 100rpx;">
							<u-avatar size="100" :src="avatarimg"></u-avatar>
						</view>
					</u-col>
				</u-row>
			</view>
			<view class="cartType_item">
				<u-row>
					<u-col :span="2">
						昵称
					</u-col>
					<u-col :span="7">
						<view style="height: 1px; width: 100%;"></view>
					</u-col>
					<u-col :span="3" text-align="center">
						<view style="color: #9E2424FF;display: inline-block;vertical-align:middle;">
							{{nickname}}
						</view>
					</u-col>
				</u-row>
			</view>
			<view class="cartType_item" @click="logout">
				<view style="color: #9E2424FF;text-align: center;">
					退出当前用户
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatarimg:'',
				nickname:uni.getStorageSync('lastUsername')
			};
		},
		onLoad() {
			uniCloud.callFunction({
				name: 'uni-id-cf_mobile',
				data: {
					action:"get_avatar"
				}
			}).then(res=>{
				this.avatarimg = res.result.userInfo.avatar
			}).catch(err=>{
				console.log(err)
			})
		},
		methods:{
			logout(){
				uni.showModal({
					content:'您确认要退出登录？',
					success(option) {
						if(option.confirm){
							uni.showLoading({
								title:'退出登录中。。'
							})
							uniCloud.callFunction({
								name: 'uni-id-cf_mobile',
								data: {
									action:"logout"
								}
							}).then(res=>{
								console.log(res)
								uni.removeStorageSync('uni_id_token')
								uni.removeStorageSync('uni_id_token_expired')
								uni.removeStorageSync('lastUsername')
								uni.removeStorageSync('userRole')
								uni.removeStorageSync('user_id')
								uni.removeStorageSync('isLogined')
								uni.removeStorageSync('isRempass')
								uni.removeStorageSync('password')
								uni.removeStorageSync('role')
								uni.removeStorageSync('count')
								uni.reLaunch({
									url:'../login/login'
								})
								uni.hideLoading()
							}).catch(err=>{
								uni.hideLoading()
								console.log(err)
							})
						}
					}
				})
			},
			setAvatar(){
				var Vue = this
				uniCloud.chooseAndUploadFile({
					type:'image',
					count:1
				}).then(res => {
					uni.showLoading({
						title:'头像设置中。。'
					})
					uniCloud.callFunction({
						name: 'uni-id-cf_mobile',
						data: {
							action:"updata_avatar",
							params:{
								avatar_url:res.tempFiles[0].url
							}
						}
					}).then(({result}) => {
						if(result.code){
							if(uni.getStorageSync('isLogined')){
								uni.hideLoading()
								this.$refs.uToast.show({
									title: 'token已过期，请登录！',
									type: 'error',
									position:"top"
								})
							}
							uni.reLaunch({
								url:'../login/login'
							})
						}else{
							uniCloud.callFunction({
								name: 'uni-id-cf_mobile',
								data: {
									action:"get_avatar"
								}
							}).then(res=>{
								uni.hideLoading()
								this.$refs.uToast.show({
									title: result.msg,
									type: 'success',
									position:"top"
								})
								this.avatarimg = res.result.userInfo.avatar
							}).catch(err=>{
								uni.hideLoading()
							})
						}
						console.log(result)
					}).catch(err => {
						uni.hideLoading()
						console.log(err)
					})
				}).catch(err =>{
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style lang="scss">
	.setting{
		position: absolute;
		width: 100%;
		min-height: 1480rpx;
		background-color: #EEEEEEFF;
		.cartType{
			.cartType_item{
				padding: 26.7rpx 32.04rpx;
				background-color: #FFFFFF;
				margin:21.36rpx 0;
				font-size: 30rpx;
			}
		}
	}
	
</style>
